package mymyoux.io.sockets.mapping
{
	import flash.utils.ByteArray;
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	import flash.utils.IExternalizable;

	[RemoteClass(alias="mymyoux.io.sockets.mapping.Telegramme")]
	public class Telegramme implements IExternalizable
	{
		private var _action:String;
		private var _object:Object;
		private var _room:String;
		private var _id:uint;
		private var _command:uint;
		private var _offset:uint;
		private var _size:uint;
		public static const EMPTY:uint = 00;
		public static const SEND:uint = 16;
		public static const LOGIN:uint = 32;
		public static const GET_ROOM:uint = 48;
		public static const LEFT_ROOM:uint = 112;
		public static const SEND_ROOM:uint = 64;
		public static const LOG_IN_ROOM:uint = 80;
		public static const LOG_OUT_ROOM:uint = 96;
		public static const LOGIN_OK:String="logok";
		public static const LOGIN_ERROR:String="logerror";
		public static const LOGIN_ALREADY_CONNECTED:String="logalreadyconnected";
		public static const ROOM_OK:String="roomok";
		public static const ROOM_FORBIDDEN:String="roomforbidden";
		public static const ROOM_EXTERIOR:String="roomext";
		public function Telegramme()
		{
			_command = 0;
			_action = "";
			_room = "";
			_id = 0;
			_offset = 0;
			_size = 0;
		}
		public function get action():String
		{
			return _action;
		}
		public function set action(value:String):void
		{
			_action = value;
		}
		public function get command():uint
		{
			return _command;
		}
		public function set command(value:uint):void
		{
			if(value & 0x01 || value & 0x02 || value & 0x04 || value & 0x08)
			{
				throw("Command has to be a multiple of 16");
			}
			_command = value;
		}
		public function get id():uint
		{
			return _id;
		}
		public function set id(value:uint):void
		{
			_id = value;
		}
		public function get object():Object
		{
			return read();
		}
		public function set object(value:Object):void
		{
			write(value);
		}
		public function get room():String
		{
			return _room;
		}
		public function set room(value:String):void
		{
			_room = value;
		}
		public function write(data:Object):void
		{
			_object = data;
		}
		public function read():Object
		{
			return _object;
		}/*
		public function toByteArray():ByteArray
		{
			var b:ByteArray = new ByteArray();
			b.writeObject(this);
			return b;
		}*/
		public function toString():String
		{
			//return "[Telegramme "+(action!=null && action.length>0?" action=\""+action+"\"":"")+(command>0?" command=\""+command+"\"":"")+(_object!=null?" value=\""+read()+"\"":"")+(_offset>0?" offset=\""+_offset+"\"":"")+"]";
			return "[Telegramme"+_toString("command")+_toString("action")+_toString("room")+_toString("id")+_toString("object")+_toString("_offset")+_toString("_size")+"]";
		}
		protected function _toString(name:String):String
		{
			var data:* = this[name];
			var valid:Boolean = false;
			if(data == null || data == undefined)
			{
				return "";
			}
			if(data is String || data is Array)
			{
				if(data.length > 0)
				{
					valid = true;
				}
			}
			else
			{
				if(data is Number)
				{
					if(data > 0)
					{
						valid = true;
					}
				}else
				{
					if(data is Object)
					{
						valid = true;
					}
				}
				
			}
			if(valid)
			{
				return " "+name+"=\""+data+"\"";
			}
			return "";
		}
		public function get value():Object
		{
			return _object;
		}
		public function set value(val:Object):void
		{
			_object = val;
		}
		public function readExternal(input:IDataInput):void
		{
			_size = input.bytesAvailable;
			_command = input.readShort();
			if(_command & 1)
			{
				_offset|=1;
				_action = input.readUTF();
			}
			if(_command & 2)
			{
				_offset|=2;
				_id = input.readShort();
			}
			if(_command & 4)
			{
				_offset|=4;
				_room = input.readUTF();
			}
			if(_command & 8)
			{
				_offset|=8;
				object = input.readObject();
			}
			_command|=15;
			_command^=15;
		}
		public function writeExternal(output:IDataOutput):void
		{
			if(_action!=null && _action.length > 0)
			{
				_command|=1;
			}
			if(_id > 0)
			{
				_command|=2;
			}
			if(_room != null && _room.length > 0)
			{
				_command|=4;
			}
			if(_object != null)
			{
				_command|=8;
			}
			
			output.writeShort(_command);
			if(_command & 1)
			{
				output.writeUTF(_action);
			}
			if(_command & 2)
			{
				output.writeShort(_id);
			}
			if(_command & 4)
			{
				output.writeUTF(_room);
			}
			if(_command & 8)
			{
				output.writeObject(_object);
			}
			_command|=15;
			_command^=15;
		}
	}
}